/*************************************************************************
	> File Name: udp_server.c
	> Author: Maureen 
	> Mail: 1437867946@qq.com 
	> Created Time: Sat 08 May 2021 04:01:01 PM CST
 ************************************************************************/

#include "head.h"

int socket_create_udp(int port) {
    int listener;
    //UDP使用数据报
    if ((listener = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return -1;
    }

    struct sockaddr_in server;
    //协议族
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(port); //指定本机的通信port

    unsigned long opt = 1;
    setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    //socket与server结构体绑定
    if (bind(listener, (struct sockaddr *)&server, sizeof(server)) < 0) {
        return -1;
    }
    return listener;
}
